package com.example.dao;

import com.example.entity.OrderInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface OrderInfoDao extends Mapper<OrderInfo> {

    @Select("SELECT * FROM order_info WHERE createTime < #{timeoutThreshold} AND status = '待付款'")
    List<OrderInfo> findOrdersByCreateTimeBefore(String timeoutThreshold);

    List<OrderInfo> findByUserId(@Param("userId") Long userId,
                                 @Param("level") Integer level);
    List<OrderInfo> findByEndUserId(@Param("userId") Long userId,
                                    @Param("status") String status,
                                    @Param("level") Integer level);

    int deleteById(Long id);

    OrderInfo findById(Long orderId);

    @Update("update order_info set status = #{status} where id = #{id}")
    void updateStatus(@Param("id") Long id, @Param("status") String status);

    @Select("select sum(totalPrice) from order_info where status = '完成'")
    Double totalPrice();

    @Select("SELECT e.`name`, SUM(e.price) as price FROM\n" +
            "(SELECT d.`name` as `name`, a.totalPrice as price FROM order_info AS a\n" +
            "JOIN order_goods_rel AS b ON a.id = b.orderId\n" +
            "JOIN goods_info AS c ON b.goodsId = c.id\n" +
            "LEFT JOIN type_info AS d ON c.typeId = d.id) as e\n" +
            "GROUP BY e.`name`")
    List<Map<String, Object>> getTypePrice();

    @Select("SELECT e.`name`, SUM(e.count) as count FROM\n" +
            "(SELECT d.`name` as `name`, b.count as count FROM order_info AS a\n" +
            "JOIN order_goods_rel AS b ON a.id = b.orderId\n" +
            "JOIN goods_info AS c ON b.goodsId = c.id\n" +
            "LEFT JOIN type_info AS d ON c.typeId = d.id) as e\n" +
            "GROUP BY e.`name`")
    List<Map<String, Object>> getTypeCount();

    List<OrderInfo> queryOrderInfoByStatus();


    OrderInfo queryOrderInfoByOrderId(String orderId);


    int updataState(String i);



    @Select("SELECT " +
            "oi.id AS order_id, " +
            "oi.totalPrice AS order_total_price, " +
            "oi.userId AS order_user_id, " +
            "oi.level AS order_level, " +
            "oi.linkAddress AS order_link_address, " +
            "oi.linkPhone AS order_link_phone, " +
            "oi.linkMan AS order_link_man, " +
            "oi.createTime AS order_create_time, " +
            "oi.orderType AS order_type, " +
            "oi.status AS order_status, " +
            "gi.id AS goods_id, " +
            "gi.name AS goods_name, " +
            "gi.description AS goods_description, " +
            "gi.fileIds AS goods_file_ids, " +
            "gi.price AS goods_price, " +
            "gi.sales AS goods_sales, " +
            "gi.hot AS goods_hot, " +
            "gi.typeId AS goods_type_id, " +
            "gi.userId AS goods_user_id, " +
            "gi.level AS goods_level, " +
            "gi.discount AS goods_discount, " +
            "gi.recommend AS goods_recommend, " +
            "gi.pic AS goods_pic, " +
            "ogr.count AS rel_count " +  // This is the count from order_goods_rel table
            "FROM order_info oi " +
            "JOIN order_goods_rel ogr ON oi.id = ogr.orderId " +
            "JOIN goods_info gi ON ogr.goodsId = gi.id")
    List<Map<String, Object>> getAllOrderInfoWithGoodsRaw();

    int placeOrder(Long id, Integer orderType);

    List<OrderInfo> queryAmbi(@Param("status") String status, @Param("orderType") Integer orderType);

    OrderInfo findLastOrderByUserId(Long userId);

    Double getAllPrice();

    Double getAllPriceByMonth();
}